<template>
  <div>
    <div v-if="this.$route.query.name || this.$route.query.id">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{
          this.$route.query.name
        }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider></el-divider>
    </div>
    <el-row type="flex" justify="space-between">
      <el-col :span="21">
        <!-- 搜索form表单 -->
        <el-form
          :inline="true"
          ref="formDate"
          :model="formDate"
          :rules="rules"
          class="demo-form-inline"
        >
          <el-form-item label="目录名称" prop="directoryName" v-if="tagDirect">
            <el-input v-model="formDate.directoryName" ></el-input>
          </el-form-item>
          <el-form-item label="标签名称" prop="tagName" v-else>
            <el-input v-model="formDate.tagName" ></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="state">
            <el-select v-model="formDate.state" placeholder="请选择">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clearFormDate">清除</el-button>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="4">
        <el-button
          type="text"
          v-if="this.$route.query.name || this.$route.query.id"
          icon="el-icon-back"
          size="small"
          @click="$router.back()"
          >返回学科</el-button
        >
        <el-button
          type="success"
          icon="el-icon-edit"
          style="float: right"
          @click="addDirectoryFn"
          v-text=" tagDirect ? '新增目录' : '新增标签'"
          ></el-button
        >
      </el-col>
      <!-- 新增目录 - 弹出框 -->
      <el-dialog
        :title="editShow ? '编辑目录' :'新增目录'"
        :visible.sync="dialogVisible"
        width="30%"
        ref='dialogVisible'
        close="closedFn"
        :close-on-click-modal='false'
        :close-on-press-escape = 'false'
      >
        <!-- 弹框表单 -->
        <el-form
          label-position="right"
          label-width="80px"
          ref="addRoluses"
          :model="addformData"
          :rules="addRoluses"
        >
          <el-form-item
            label="所属学科"
            prop='subjectID'
            v-if="!this.$route.query.name || !this.$route.query.id"
          >
            <el-select v-model="addformData.subjectID" placeholder="请选择"
            style="width:100%">
              <el-option v-for='obj in subjectNameList' :key="obj.value" :label="obj.label" :value="obj.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName" v-if="tagDirect">
            <el-input
              v-model="addformData.directoryName"
              placeholder="请输入目录名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName" v-else>
            <el-input
              v-model="addformData.tagName"
              placeholder="请输入目录名称"
            ></el-input>
          </el-form-item>
          <el-form-item class="buttonStyle">
            <el-button @click="cancelAddDirectoryFn"
              >取消</el-button
            >
            <el-button  type="primary" @click="sureAddDirectoryFn">确定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
// 引入目录添加APi
import { add as addformDataAPI, update as editDirectorysAPI } from '@/api/hmmm/directorys'
// 引入学科列表API
import { simple as getSubjectNameListAPI } from '@/api/hmmm/subjects'
// 引入标签
import { add as addtabgsformDataAPI, update as edittabgsDirectorysAPI } from '@/api/hmmm/tags'
export default {
  name: 'DirectoryTagSearch',
  props: {
    query: {
      type: Object
    }
  },
  created () {
    // 获取学科下拉列表
    this.addSubjectNameListFn()
    // 从学科传参过来 - 需要将其id收集
    // 并将其为搜索选项 - 渲染列表
    if (this.$route.query.name || this.$route.query.id) {
      // 需要的数值类型 -> 强制转换
      this.addformData.subjectID = Number(this.$route.query.id)
      this.formDate.subjectID = Number(this.$route.query.id)
      // 收集学科id -> 传给父组件 -> 渲染数据
      this.$nextTick(() => {
        this.$emit('directorysListFn', { subjectID: this.$route.query.id })
      })
    }
    // console.log(this.$route.path)
    if (this.$route.path === '/subjects/tags') {
      this.tagDirect = false
    }
  },
  data () {
    return {
      // 搜索表单数据
      formDate: {
        tagName: null,
        subjectID: null,
        directoryName: null, // 目录名称
        state: null // 状态
      },
      tagDirect: true, // 确定是哪个页面路由 true为目录
      dialogVisible: false, // 目录新增弹框 true为编辑
      // 新增目录
      addformData: {
        subjectID: null, // 所属学科
        directoryName: '' // 目录名称
      },
      editShow: false, // 控制编辑和新增
      subjectNameList: [], // 学科列表
      // 搜索表单 - 正则校验
      rules: {
      },
      // 弹框表单 - 正则校验
      addRoluses: {
        directoryName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取学科下拉列表
    async addSubjectNameListFn () {
      // 因为默认的是10条
      // 获取拿到学科种类总数 -> 赋值给页数 ->重新获取所有的列表
      // 渲染下拉列表
      // 有简单列表 - 上面不需要操作
      const res = await getSubjectNameListAPI()
      this.subjectNameList = res.data
      console.log('学科列表', res.data)
    },
    // 封装 - 修改目录API方法
    async editDirectorysFn () {
      if (this.tagDirect) {
        await editDirectorysAPI(this.addformData).then(() => {
          this.$message.success('修改成功')
        })
      } else {
        await edittabgsDirectorysAPI(this.addformData).then(() => {
          this.$message.success('修改成功')
        })
      }
    },
    // 目录 - 添加 - (API接口)
    async addformDataFn () {
      this.query.page = 1

      if (this.tagDirect) {
        await addformDataAPI(this.addformData).then(() => {
          this.$message.success('添加成功')
        })
      } else {
        await addtabgsformDataAPI(this.addformData).then(() => {
          this.$message.success('添加成功')
        })
      }
    },
    // 搜索 - 按键
    onSubmit () {
      this.query.page = 1;
      // 兜底判断 -> 满足 -> 传表单数据给父组件
      (this.formDate.tagName || this.formDate.directoryName || this.formDate.state || this.$route.query.id) ? this.$emit('searchFormData', this.formDate) : this.$emit('searchFormData')
    },
    // 清除 - 搜索表单
    clearFormDate () {
      // 重置表单
      this.$refs.formDate.resetFields()
      // 清除重新获取列表
      // this.$emit('directorysListFn')
    },
    // 按键 - 新增目录
    addDirectoryFn () {
      this.editShow = false
      this.dialogVisible = true
    },
    // 弹出层 - 按键 - 取消
    cancelAddDirectoryFn () {
      this.closedFn()
    },
    // 弹出层 - 按键 - 确定
    sureAddDirectoryFn () {
      // 兜底判断 -> 满足 -> 发送请求 -> 关闭弹框 -> 清空表单 -> 更新列表
      this.$refs.addRoluses.validate(async (valid) => {
        if (!valid) return
        // await this.addformDataFn()
        this.editShow ? await this.editDirectorysFn() : await this.addformDataFn()
        await this.$emit('directorysListFn', { ...this.query, ...this.formDate })
        await this.closedFn()
      })
    },
    // 弹框 - 弹框关闭的回调
    closedFn () {
      this.dialogVisible = false
      this.addformData = {
        subjectID: this.$route.query.id || '', // 所属学科
        directoryName: '' // 目录名称
      }
      this.$refs.addRoluses.resetFields()
    }
  }
}
</script>

<style scoped>
.el-divider--horizontal {
  margin: 16px 0;
}
.el-breadcrumb {
  cursor: pointer;
}
.el-breadcrumb__inner is-link {
  color: yellow;
}
.buttonStyle /deep/ .el-form-item__content {
  text-align: center;
  margin-left: 0 !important;
}
</style>
